import axios from 'axios'
import { useAdminInfoStore } from '../store'

// axios请求实例
const instance = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL as string, // 接口统一域名
    timeout: 1000000, // 设置超时
    headers: {
        'Content-Type': 'application/json;charset=UTF-8;',
    }
})

// 请求拦截器
instance.interceptors.request.use(config => {
    // 定义登录接口路径
    const loginUrl = '/admin/login';
    const registerUrl = '/admin/register'
    if (config.url != loginUrl && config.url != registerUrl) {
        const adminInfoStore = useAdminInfoStore();
        const token = adminInfoStore.getToken;
        const chatSessionId = adminInfoStore.getChatSessionId;
        if (token) {
            // 在请求头中添加 Token
            config.headers['token'] = token;
        }
        if (chatSessionId) {
            // 在请求头中添加chatSessionId
            config.headers['chatSessionId'] = chatSessionId;
        }
    }
    return config;
}, error => {
    return Promise.reject(error);
});

export default instance;